package cn.xfdzx.android.apps.shop.adapter.remake;

import android.graphics.drawable.Drawable;
import android.media.Image;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.luck.picture.lib.entity.LocalMedia;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.adapter.FullyGridLayoutManager;
import cn.xfdzx.android.apps.shop.adapter.GridImageSelAdapter;
import cn.xfdzx.android.apps.shop.bean.OrderListBeen;
import cn.xfdzx.android.apps.shop.util.EmptyUtil;
import cn.xfdzx.android.apps.shop.util.ImageGlideUtil;
import cn.xfdzx.android.apps.shop.widget.CornerImageView;

/**
 * Creator: zf.
 * Date: 2020/9/11.
 * Description:批发订单评价商品列表适配器.
 */
public class OrderRemakeGoodsMarketAdapter extends BaseQuickAdapter<OrderListBeen.DataBean.CartInfoBean, BaseViewHolder>  {
    private OnItemImgSelListener onItemImgSelListener;
    //图片上传相关
    private FullyGridLayoutManager managerimg;
    private GridImageSelAdapter adapter;
    private List<OrderListBeen.DataBean.CartInfoBean> cartInfo;//商品列表
    private List<OrderListBeen.DataBean.CartInfoBean> cartInfoSave;//用于显示或者隐藏更多列表
    public boolean is_open = false;
    public OrderRemakeGoodsMarketAdapter(@Nullable List<OrderListBeen.DataBean.CartInfoBean> data,
                                         List<OrderListBeen.DataBean.CartInfoBean> cartInfoSave) {
        super(R.layout.item_remake_market, data);
        this.cartInfo = data;
        this.cartInfoSave = cartInfoSave;
    }

    @Override
    public void setData(int index, @NonNull OrderListBeen.DataBean.CartInfoBean data) {
        super.setData(index, data);
    }

    @Override
    protected void convert(final BaseViewHolder viewHolder, final OrderListBeen.DataBean.CartInfoBean date) {
        ImageView star01 = viewHolder.getView(R.id.star01);
        ImageView star02 = viewHolder.getView(R.id.star02);
        ImageView star03 = viewHolder.getView(R.id.star03);
        ImageView star04 = viewHolder.getView(R.id.star04);
        ImageView star05 = viewHolder.getView(R.id.star05);

        RecyclerView recycler_img = viewHolder.getView(R.id.recycler_img);
        TextView tv_zt_state = viewHolder.getView(R.id.tv_zt_state);
        EditText ed_des = viewHolder.getView(R.id.ed_des);
        //圆角图片框架
        CornerImageView imageView = viewHolder.getView(R.id.img_goods);
        imageView.setType(CornerImageView.TYPE_ROUND);

        ImageGlideUtil.loadImage(mContext,imageView,date.getProductInfo().getImage());
        managerimg = new FullyGridLayoutManager(mContext, 3, GridLayoutManager.VERTICAL, false);

        /**
         * 上传图片列表
         * */
        //图片选择结果列表
        List<LocalMedia> selectList = null;
        if (EmptyUtil.isNotEmpty(date.getSelectList())){
            selectList = date.getSelectList();
        }else {
            selectList = new ArrayList<>();
        }

        recycler_img.setLayoutManager(managerimg);

        adapter = new GridImageSelAdapter(mContext, new GridImageSelAdapter.onAddPicClickListener() {
        @Override
        public void onAddPicClick() {
            onItemImgSelListener.onItemImgSelClick(viewHolder.getLayoutPosition(),date);
        }
        });

        adapter.setList(selectList);

        adapter.setSelectMax(5);
        recycler_img.setAdapter(adapter);

        viewHolder.setText(R.id.tv_goodstitle,date.getProductInfo().getStore_name())
                .setText( R.id.tv_specifications,"包装规格: "+date.getProductInfo().getUnit_name());
        if (date.getStarNum()==0){//默认显示5星
            setStarLight(star01,star02,star03,star04,star05,"5",tv_zt_state);
        }else {
            setStarLight(star01,star02,star03,star04,star05,date.getStarNum()+"",tv_zt_state);
        }


        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("itemView","itemView");
            }
        });

        ed_des.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().isEmpty()) {
                    //tv_count.setText("0/500");
                } else {
                    //tv_count.setText(editable.toString().length()+"/500");
                    cartInfo.get(viewHolder.getLayoutPosition()).setRemakeContent(s.toString());
                    if (viewHolder.getLayoutPosition() == 0){
                        cartInfoSave.get(0).setRemakeContent(s.toString());
                    }

                }
            }
        });

        //Edittext通知父控件自己处理自己的滑动事件
        ed_des.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (v.getId() == R.id.ed_des && canVerticalScroll(ed_des)) {
                    v.getParent().requestDisallowInterceptTouchEvent(true);
                    if (event.getAction() == MotionEvent.ACTION_UP) {
                        v.getParent().requestDisallowInterceptTouchEvent(false);
                    }
                }
                return false;
            }
        });

        star01.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cartInfo.get(viewHolder.getLayoutPosition()).setStarNum(1);
                setStarLight(star01,star02,star03,star04,star05,"1",tv_zt_state);
                if (viewHolder.getLayoutPosition() == 0){
                    cartInfoSave.get(0).setStarNum(1);
                }
            }
        });
        star02.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cartInfo.get(viewHolder.getLayoutPosition()).setStarNum(2);
                setStarLight(star01,star02,star03,star04,star05,"2",tv_zt_state);
                if (viewHolder.getLayoutPosition() == 0){
                    cartInfoSave.get(0).setStarNum(2);
                }
            }
        });
        star03.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cartInfo.get(viewHolder.getLayoutPosition()).setStarNum(3);
                setStarLight(star01,star02,star03,star04,star05,"3",tv_zt_state);
                if (viewHolder.getLayoutPosition() == 0){
                    cartInfoSave.get(0).setStarNum(3);
                }
            }
        });
        star04.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cartInfo.get(viewHolder.getLayoutPosition()).setStarNum(4);
                setStarLight(star01,star02,star03,star04,star05,"4",tv_zt_state);
                if (viewHolder.getLayoutPosition() == 0){
                    cartInfoSave.get(0).setStarNum(4);
                }
            }
        });
        star05.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cartInfo.get(viewHolder.getLayoutPosition()).setStarNum(5);
                setStarLight(star01,star02,star03,star04,star05,"5",tv_zt_state);
                if (viewHolder.getLayoutPosition() == 0){
                    cartInfoSave.get(0).setStarNum(5);
                }
            }
        });
    }

    private void setDrawableLeft(TextView tv_cai,Drawable drawable) {
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        tv_cai.setCompoundDrawables(drawable, null, null, null);
    }

    public void clearDatas(){
        mData.clear();
        notifyDataSetChanged();
    }
    public void refreshDatas(List<OrderListBeen.DataBean.CartInfoBean> list){
        super.replaceData(list);
    }

    public void setItemImgSelListener(OnItemImgSelListener listener){
        this.onItemImgSelListener = listener;
    }

    public interface OnItemImgSelListener {
        void onItemImgSelClick(int position, OrderListBeen.DataBean.CartInfoBean date);
    }

    @Override
    public int getItemCount() {
        if (mData.size() > 1) {
            if (!is_open) {
                return 1;
            } else {
                return mData.size();
            }
        }
        return mData.size();
    }

    //    private GridImageSelAdapter.onAddPicClickListener onAddPicClickListener = new GridImageSelAdapter.onAddPicClickListener() {
//        @Override
//        public void onAddPicClick() {
//            onItemImgSelListener.onItemImgSelClick();
//        }
//
//    };


    /**
     * EditText竖直方向是否可以滚动
     *
     * @param //editText需要判断的EditText
     * @return true：可以滚动   false：不可以滚动
     */
    private boolean canVerticalScroll(EditText contentEt) {
        //滚动的距离
        int scrollY = contentEt.getScrollY();
        //控件内容的总高度
        int scrollRange = contentEt.getLayout().getHeight();
        //控件实际显示的高度
        int scrollExtent = contentEt.getHeight() - contentEt.getCompoundPaddingTop() - contentEt.getCompoundPaddingBottom();
        //控件内容总高度与实际显示高度的差值
        int scrollDifference = scrollRange - scrollExtent;

        if (scrollDifference == 0) {
            return false;
        }
        return (scrollY > 0) || (scrollY < scrollDifference - 1);
    }


    /**设置星星点亮的方法*/
    private void setStarLight(ImageView img_star1,ImageView img_star2,ImageView img_star3,
                              ImageView img_star4,ImageView img_star5,String star,TextView tv_zt_state) {
        switch (star){
            case "1":
                Log.e("","");
                clearStarLight(img_star1,img_star2,img_star3,img_star4,img_star5);
                img_star1.setImageResource(R.drawable.ic_staryellow);
                img_star1.setFocusable(true);

                Animation ani = AnimationUtils.loadAnimation(mContext, R.anim.anim_star_scale);
                img_star1.startAnimation(ani);//開始动画

                //tv_zt_state.setVisibility(View.VISIBLE);
                tv_zt_state.setText("非常差");
                tv_zt_state.setFocusable(true);
                tv_zt_state.requestFocus();
                break;
            case "2":
                clearStarLight(img_star1,img_star2,img_star3,img_star4,img_star5);
                img_star1.setImageResource(R.drawable.ic_staryellow);
                img_star2.setImageResource(R.drawable.ic_staryellow);
                img_star1.setFocusable(true);
                Animation anim1 = AnimationUtils.loadAnimation(mContext, R.anim.anim_star_scale);
                img_star1.startAnimation(anim1);//開始动画
                Animation anim2 = AnimationUtils.loadAnimation(mContext, R.anim.anim_star_scale);
                img_star2.startAnimation(anim2);//開始动画

                //tv_zt_state.setVisibility(View.VISIBLE);
                tv_zt_state.setText("差");
                tv_zt_state.setFocusable(true);
                tv_zt_state.requestFocus();
                break;
            case "3":
                clearStarLight(img_star1,img_star2,img_star3,img_star4,img_star5);
                img_star1.setImageResource(R.drawable.ic_staryellow);
                img_star2.setImageResource(R.drawable.ic_staryellow);
                img_star3.setImageResource(R.drawable.ic_staryellow);
                tv_zt_state.setFocusable(true);
                tv_zt_state.requestFocus();
                Animation anima1 = AnimationUtils.loadAnimation(mContext, R.anim.anim_star_scale);
                img_star1.startAnimation(anima1);//開始动画
                Animation anima2 = AnimationUtils.loadAnimation(mContext, R.anim.anim_star_scale);
                img_star2.startAnimation(anima2);//開始动画
                Animation anima3 = AnimationUtils.loadAnimation(mContext, R.anim.anim_star_scale);
                img_star3.startAnimation(anima3);//開始动画

                //tv_zt_state.setVisibility(View.VISIBLE);
                tv_zt_state.setText("一般");
                break;
            case "4":
                clearStarLight(img_star1,img_star2,img_star3,img_star4,img_star5);
                img_star1.setImageResource(R.drawable.ic_staryellow);
                img_star2.setImageResource(R.drawable.ic_staryellow);
                img_star3.setImageResource(R.drawable.ic_staryellow);
                img_star4.setImageResource(R.drawable.ic_staryellow);
                tv_zt_state.setFocusable(true);
                tv_zt_state.requestFocus();
                Animation animation1 = AnimationUtils.loadAnimation(mContext, R.anim.anim_star_scale);
                img_star1.startAnimation(animation1);//開始动画
                Animation animation2 = AnimationUtils.loadAnimation(mContext, R.anim.anim_star_scale);
                img_star2.startAnimation(animation2);//開始动画
                Animation animation3 = AnimationUtils.loadAnimation(mContext, R.anim.anim_star_scale);
                img_star3.startAnimation(animation3);//開始动画
                Animation animation4 = AnimationUtils.loadAnimation(mContext, R.anim.anim_star_scale);
                img_star4.startAnimation(animation4);//開始动画

                //tv_zt_state.setVisibility(View.VISIBLE);
                tv_zt_state.setText("满意");
                break;
            case "5":
                img_star1.setFocusable(true);
                img_star1.requestFocus();
                img_star1.setImageResource(R.drawable.ic_staryellow);
                img_star2.setImageResource(R.drawable.ic_staryellow);
                img_star3.setImageResource(R.drawable.ic_staryellow);
                img_star4.setImageResource(R.drawable.ic_staryellow);
                img_star5.setImageResource(R.drawable.ic_staryellow);
                tv_zt_state.setFocusable(true);
                tv_zt_state.requestFocus();
                Animation animations1 = AnimationUtils.loadAnimation(mContext, R.anim.anim_star_scale);
                img_star1.startAnimation(animations1);//開始动画
                Animation animations2 = AnimationUtils.loadAnimation(mContext, R.anim.anim_star_scale);
                img_star2.startAnimation(animations2);//開始动画
                Animation animations3 = AnimationUtils.loadAnimation(mContext, R.anim.anim_star_scale);
                img_star3.startAnimation(animations3);//開始动画
                Animation animations4 = AnimationUtils.loadAnimation(mContext, R.anim.anim_star_scale);
                img_star4.startAnimation(animations4);//開始动画
                Animation animations5 = AnimationUtils.loadAnimation(mContext, R.anim.anim_star_scale);
                img_star5.startAnimation(animations5);//開始动画

                //tv_zt_state.setVisibility(View.VISIBLE);
                tv_zt_state.setText("非常满意");
                break;
            default:
                clearStarLight(img_star1,img_star2,img_star3,img_star4,img_star5);
                img_star1.setImageResource(R.drawable.ic_staryellow);
                break;
        }
    }

    private void clearStarLight(ImageView img_star1,ImageView img_star2,ImageView img_star3,ImageView img_star4,ImageView img_star5) {
        img_star1.setImageResource(R.drawable.ic_stargray);
        img_star2.setImageResource(R.drawable.ic_stargray);
        img_star3.setImageResource(R.drawable.ic_stargray);
        img_star4.setImageResource(R.drawable.ic_stargray);
        img_star5.setImageResource(R.drawable.ic_stargray);
    }
}
